/****************************** Module Header ******************************\
 * Module Name:  mxInteger.cs
 * Module Version:  1.0.0 (2011-11-4)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension Integer value's method.  It supplies 
 * useful extension methods, such as ParseToBoolean().
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UCL
{
    /// <summary>
    /// 数值的扩展类
    /// <para>提供数值类型的扩展方法，如ParseToBoolean()</para>
    /// </summary>
    public static class mxInteger
    {
        /// <summary>
        /// 转换成布尔值
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static bool ParseToBoolean(this int i)
        {
            return i == 0 ? false : true;
        }

        /// <summary>
        /// 是否在其中
        /// </summary>
        /// <param name="i"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static bool IsIn(this int i, params int[] args)
        {
            return args.Contains(i);
        }

        /// <summary>
        /// 获取非空值
        /// </summary>
        /// <param name="i"></param>
        /// <param name="defv"></param>
        /// <returns></returns>
        public static int NotNull(this int? i, int defv)
        {
            return i.HasValue ? i.Value : defv;
        }

        /// <summary>
        /// 数值转换成时间(单位秒)
        /// <para>如：ToTime(3661)为"01:01:01"</para>
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static string ToTime(this int v)
        {
            int s = v % 60;
            int m = v / 60 % 60;
            int h = v / 3600;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendFormat("{0}{1}", h < 10 ? "0" : "", h);
            sb.Append(":");
            sb.AppendFormat("{0}{1}", m < 10 ? "0" : "", m);
            sb.Append(":");
            sb.AppendFormat("{0}{1}", s < 10 ? "0" : "", s);
            return sb.ToString();
        }

        /// <summary>
        /// 用于分页计算，a=recordcount,b=pagesize，返回页数 2013-6-10 by sutroon
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int Ceil(this int a, int b)
        {
            int c;
            if (a == 0) return 0;
            c = a / b;
            if (a % b > 0) c += 1;
            return c;
        }
    }
}
